﻿using DevLib.TestCase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/** 
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 * Description: 
 * Author: Heller (HellerSong@outlook.com)
 * CreationTime: 3/4/2015 2:14:32 PM
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
 **/
namespace DevLib.FileTool
{
    public class DirOM
    {
        private DirOM() { }


        public static String CombinePath(String path1, String path2, params String[] paths)
        {
            String resultPath;

            /*
			 * Do the arguments check
			 */
            if ((path1 == null) || (path2 == null))
            {
                Console.WriteLine("Argument should not NULL:" + ((path1 == null) ? "path1" : "path2"));
                return null;
            }
            StrCheck.DoesPathContainInvalidChars(path1);
            StrCheck.DoesPathContainInvalidChars(path2);

            /*
			 * Do the operaction
			 */
            if (!path1.EndsWith(Path.DirectorySeparatorChar + ""))   // Not endwith '\'
                path1 = path1 + Path.DirectorySeparatorChar;
            resultPath = Path.Combine(path1, path2);
            for (int i = 0; i < paths.Length; i++)
            {
                StrCheck.CheckChainDirIllegal(paths[i]);
                resultPath = Path.Combine(resultPath, paths[i]);
            }
            if (resultPath.EndsWith(Path.DirectorySeparatorChar + ""))
                resultPath = resultPath.Remove(resultPath.Length - 1);  // Remove last '\'

            return resultPath;
        }
        public static String CombineUrl(String path1, String path2, params String[] paths)
        {
            String resultPath;

            /*
			 * Do the argument check
			 */
            if ((path1 == null) || (path2 == null))
            {
                Console.WriteLine("Argument should not NULL:" + ((path1 == null) ? "path1" : "path2"));
                return null;
            }
            StrCheck.DoesPathContainInvalidChars(path1);
            StrCheck.DoesPathContainInvalidChars(path2);

            /*
			 * Do the operaction
			 */
            if (!path1.EndsWith("/"))
                path1 = path1 + "/";
            resultPath = path1 + path2;
            for (int i = 0; i < paths.Length; i++)
            {
                StrCheck.CheckChainDirIllegal(paths[i]);
                if (!resultPath.EndsWith("/"))
                    resultPath = resultPath + "/";
                resultPath = resultPath + paths[i];
            }

            if (resultPath.EndsWith("/"))
                resultPath = resultPath.Remove(resultPath.Length - 1);  // Remove last '/'
            resultPath = resultPath.Replace('\\', '/'); // Replace all '\' to '/'

            return resultPath;
        }

        public static bool IsDirectory(String path)
        {
            StrCheck.CheckChainDirIllegal(path);

            if (Directory.Exists(path))
                return true;
            else
                return false;
        }

        public static String GetLeafDirectoryName(String path)
        {
            StrCheck.CheckChainDirIllegal(path);

            if (path.EndsWith(Path.DirectorySeparatorChar + ""))
                path = path.Remove(path.Length - 1);

            return path.Substring(path.LastIndexOf(Path.DirectorySeparatorChar) + 1);
        }
        public static String GetLastSubDirectory(String path, int recursionIndex)
        {
            StrCheck.CheckChainDirIllegal(path);

            if (recursionIndex <= 0)
            {
                Console.WriteLine("Recursion Index shoulc >= 1");
                return null;
            }

            if (path.EndsWith(Path.DirectorySeparatorChar + ""))   // Endwith '\'
                path = path.Remove(path.Length - 1);

            char[] chars = path.ToCharArray();
            int appearCount = 0;
            int i = chars.Length - 1;
            while (i >= 0)
            {
                if (chars[i] == Path.DirectorySeparatorChar)
                {
                    appearCount++;
                    if (appearCount >= recursionIndex)
                        break;
                }
                i--;
            }

            return path.Substring(i);
        }
        public static String GetParrentDirectory(String path)
        {
            StrCheck.CheckChainDirIllegal(path);

            return Directory.GetParent(path).ToString();
        }
        public static String GetParrentDirectory(String path, int recursionIndex)
        {
            StrCheck.CheckChainDirIllegal(path);

            if (recursionIndex < 0)
            {
                Console.WriteLine("Recursion Index shoulc >= 0");
                return null;
            }

            if (recursionIndex == 0)
                return path;
            else
                return GetParrentDirectory(Directory.GetParent(path).ToString(), recursionIndex - 1);
        }

        public static void MakeDirectory(String path)
        {
            StrCheck.CheckChainDirIllegal(path);

            // [Upgrade] use secitiry way
            if (!Directory.Exists(path))   // get the permision of the folder, or can make dir in C:\?
                Directory.CreateDirectory(path);
        }
        public static void RenameDirectory(String path, String newName)
        {
            StrCheck.CheckChainDirIllegal(path);
            StrCheck.DoesPathContainInvalidChars(newName);

            // [Upgrade] Need look for a better solution, not move and delete
            String newDirPath = Path.Combine(GetParrentDirectory(path), newName);
            Directory.Move(path, newDirPath);
            //FileOM.deleteWholeDirectory(path);
        }


    }
}
